package fi.hell.lunchit

import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

class Company {
	
	static hasMany = ["employees":User]

    byte[] logo
	String name
	String address
	String zip
	String city
	String country
    int luncheonVoucherTotalValue = 0
    int luncheonVoucherProfitValue = 0

    static constraints = {
		name	(nullable:false, blank:false)
		address (nullable:false, blank:false)
		zip		(nullable:false, blank:false)
		city	(nullable:false, blank:false)
		country	(nullable:false, blank:false)
        logo    (maxSize:1048576 /* 1mb */ , validator: { val, obj ->
			InputStream bais = new ByteArrayInputStream(val)
			BufferedImage image = ImageIO.read(bais)

			if (image?.getWidth() != (CH.config.company.logo.width as int) || image?.getHeight() != (CH.config.company.logo.height as int)) {
				return ['restaurant.invalid.logoDimensions', CH.config.company.logo.width, CH.config.company.logo.height]
			}
		})
    }

    String toString() {
        return this.name
    }
}
